#include <QPainter>
#include <QDebug>
#include <QMouseEvent>
#include <QLineEdit>
#include <QMovie>

#include "MessageData.h"
#include "PopupItemModel.h"

#include "PopupItem.h"

const QSize AvatarSize(35,35);
int NickNameOffset = 0;

PopupItemDelegate::PopupItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent),m_countSize(20,20)
{

}

PopupItemDelegate::~PopupItemDelegate()
{
}

void PopupItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();//每个绘制都仅保证影响自己区域
    // 其实内部是一个超大的场景图，坐标通过option来获取当前绘制区域
    const MessageData &data = index.data().value<MessageData>();

    painter->setRenderHints(QPainter::Antialiasing |QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
    int xOffset,yOffset;
    xOffset = option.rect.x();
    yOffset = option.rect.y();

    painter->drawPixmap(QRect(xOffset,yOffset,AvatarSize.width(),AvatarSize.height()),data.avatar.scaled(AvatarSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));

    xOffset += AvatarSize.width() + 5;

    if (data.ext) {
        QPixmap gifPixmap = data.ext->currentPixmap();

//        qDebug() << "update" << index;
        do {
            if (gifPixmap.isNull())
                break;
            painter->drawPixmap(QRect(xOffset, yOffset, AvatarSize.width(), AvatarSize.height()), gifPixmap.scaled(AvatarSize,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));

            xOffset += AvatarSize.width() + 5;
        } while(0);
    }

    QFontMetrics metri = painter->fontMetrics();

    QSize nameSize(option.rect.width() - AvatarSize.width() - m_countSize.width() * 2,14);

    NickNameOffset = xOffset;

    painter->drawText(QRect(xOffset,yOffset,nameSize.width(),nameSize.height()),data.name);

    QSize contentSize = nameSize;

    painter->drawText(QRect(xOffset,yOffset + option.rect.height()/2,contentSize.width(),contentSize.height()),data.content);

    xOffset += nameSize.width() + 5;

    painter->setPen(Qt::darkRed);
    painter->setBrush(Qt::darkRed);

    QRectF countRect(xOffset,yOffset + (option.rect.height() - m_countSize.height())/2.0,m_countSize.width(),m_countSize.height());

    painter->drawRoundedRect(countRect,m_countSize.height()/2.0,m_countSize.height()/2.0);

    QRect fontRect;
    fontRect.setWidth(metri.width(QString::number(data.count)));
    fontRect.setHeight(metri.height());

    painter->setPen(Qt::white);
    painter->drawText(QRectF(countRect.x() + (countRect.width() - fontRect.width())/2.0,countRect.y() + (countRect.height() - fontRect.height())/2.0
                             ,countRect.width(),countRect.height()),QString::number(data.count));
    painter->restore();
}

bool PopupItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    // 这里进行每个item的事件处理,比如点击事件等
    if (event->type() == QMouseEvent::MouseButtonPress) {
        emit clickItem(index);
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

QSize PopupItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    // 可以使用默认宽度，加上自己的高度
    return QSize(option.rect.size().width(),40);
}

QWidget *PopupItemDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
                                         const QModelIndex & index) const

{
    const MessageData &data = index.data().value<MessageData>();
    // 触发编辑事件：createEditor》setEditorData，当编辑完成（按下enter或者点击其他item，setModelData

    QLineEdit *editor = new QLineEdit(parent);
    editor->setAttribute(Qt::WA_MacShowFocusRect, 0);
    return editor;
}

void PopupItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QLineEdit *e = dynamic_cast<QLineEdit*>(editor);
    const MessageData &data = index.data().value<MessageData>();
    e->setText(data.name);
}

void PopupItemDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const
{
    PopupItemModel *data = dynamic_cast<PopupItemModel*>(model);
    if (data) {
        QLineEdit *line = dynamic_cast<QLineEdit*>(editor);
        data->updateData(index.row(),line->text());
    }
}

void PopupItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    int x = option.rect.x() + NickNameOffset - 4;
    int y = option.rect.y() - 1;
    editor->setGeometry(QRect(x,y,option.rect.width()/2,option.rect.height()/2));
}
